import numpy as np
from numpy.ma.extras import average

lst = [1, 2, 3]
print(lst)
arr = np.array(lst)
print(arr)

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)

arr = np.zeros((5, 6))
print(arr)
arr = np.ones((3, 3))
print(arr)
arr = np.full((5, 6), 1)
print(arr)
arr = np.linspace(0, 1, 10)
print(arr)
arr = np.arange(0, 10, 2)
print(arr)
arr = np.random.randint(10, size=20).reshape(5, 4)
print(arr)
np.save("data", arr)
arr1 = np.load("data.npy")
print(arr1)
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.ndim)
print(arr)
print(arr.shape)
print(arr.dtype)
arr = np.array([1, 2, 3, 4, 5])
print(arr[0])
print(arr[-1])
print(arr[len(arr) - 1])
print(arr)
print(arr[1:])
print(arr[-1:0:-1])
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr)
print(arr[1:, 0:-1])
print(arr[:, 1])
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr1 + arr2)
print(arr1 - arr2)
print(arr1 * arr2)
print(arr1 / arr2)
print(arr1 + 3)
print(arr1 * 3)
arr3 = np.array([[1, 2, 3], [4, 5, 6]])
arr4 = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])
arr5 = arr3.dot(arr4)
arr5 = np.dot(arr3, arr4)
print(arr5)
arr = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])
print(arr)
print(np.sum(arr))
print(np.mean(arr))
print(np.std(arr))
print(np.max(arr))
print(np.min(arr))
print(np.mean(arr[1, :]))
print(np.std(arr[1, :]))

arr = np.array([3, 4, 5, 6, 7, 8, 9, 10])
print(arr)
arr1 = arr.reshape((2, 4))
print(arr1)
arr2 = arr.reshape((2, 2, 2))
print(arr2)
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[7, 8, 9], [10, 11, 12]])
arr3 = np.hstack((arr1, arr2))
print(arr3)
arr4 = np.vstack((arr1, arr2))
print(arr4)

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
arr1 = arr > 3
print(arr1)
print(arr[arr1])
arr2 = arr[arr1]
print(arr2)

arr = np.array([[3, 4], [7, 8]])
arr1 = arr.T
print(arr)
print(arr1)
arr2 = np.linalg.inv(arr)
print(arr2)
"""
假设你是一位投资者，想要分析某几只股票在一段时间内的收益率情况。
通过计算股票的日收益率、平均收益率和收益率的标准差，你可以评估这些股票的表现和风险程度。
标准差越大，说明股票的收益率波动越大，风险也就越高。
"""
# 假设我们有 3 只股票，在 10 个交易日内的收盘价数据
# 每一行代表一只股票，每一列代表一个交易日
closing_prices = np.array([
    [100, 102, 105, 103, 106, 108, 109, 110, 107, 109],
    [200, 202, 205, 203, 206, 208, 209, 210, 207, 209],
    [300, 302, 305, 303, 306, 308, 309, 310, 307, 309]
])
#收益率 = （今天收盘价 - 昨日收盘价） / 昨日收盘价
#计算每只股票的日收益率
daily_returns = (closing_prices[:, 1:] - closing_prices[:, :-1]) / closing_prices[:, :-1]
print(daily_returns)
#计算三个股票的平均收益率
average = np.mean(daily_returns, axis=1)
print(average)
#每只股票的标准差
std_daily = np.std(daily_returns, axis=1)
print(std_daily)

for i in range(len(average)):
    print(f'第{i + 1}只股票的平均收益率为{average[i]:.4f}')
    print(f'第{i + 1}只股票的标准差为{std_daily[i]:.4f}')